package com.example.todolist.controller;

import com.example.todolist.entity.*;
import com.example.todolist.mapper.ReviewTaskMapper;
import com.example.todolist.mapper.TaskMapper;
import com.example.todolist.mapper.UserMapper;
import com.example.todolist.service.HabitService;
import com.example.todolist.service.MailService;
import com.example.todolist.service.TaskEachDayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

@RestController
@EnableScheduling
public class AutoController {
    private final UserMapper userMapper;
    private final MailService mailService;

    @Autowired
    private ReviewTaskMapper reviewTaskMapper;
    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private HabitService habitService;
    @Autowired
    private TaskEachDayService taskEachDayService;

    public AutoController(UserMapper userMapper, MailService mailService) {
        this.userMapper = userMapper;
        this.mailService = mailService;
    }
    @Scheduled(cron = "0 0 0 * * ?")
    public void setHabit() {
        List<User> users = userMapper.findAll();
        for (User user : users) {
            List<Habit> habits = habitService.getAllHabit(user.getUserId());
            for (Habit habit : habits) {
                habitService.setHabit(user.getUserId(), habit.getHabitId());
            }
        }
    }
    @Scheduled(cron = "0 30 0 * * ?")
    public void setTaskEachDay()
    {
        Date date = Date.valueOf(LocalDate.now());
        String weekDay = habitService.getWeekDay(date);
        if(Objects.equals(weekDay, "DAY1"))
        {
            List<User> users= userMapper.findAll();
            for (User user : users) {
                taskEachDayService.resetTaskEachDay(user.getUserId());
            }
        }
    }

    @Scheduled(cron = "0 0 6 * * ?")
    public void remind()
    {
        List<User> users=userMapper.findAll();
        Date today= Date.valueOf(LocalDate.now());
        for (User user : users) {
            List<ReviewTask> reviewTasks = reviewTaskMapper.getOneDayTask(user.getUserId(),today);
            for (ReviewTask reviewTask : reviewTasks) {
                Mail mail = mailService.getRemindMessage(reviewTask.getTaskName(), user.getEmail(), user.getName());
                mailService.sendMailMessage(mail);
            }

            List<Task> tasks = taskMapper.getALLTask(user.getUserId());
            for(Task task:tasks){
                if(task.getEndDate()==today)
                {
                    Mail mail = mailService.getTaskMessage(task.getTaskName(),user.getEmail(),user.getName());
                    mailService.sendMailMessage(mail);
                }
            }

        }
    }
//    @Scheduled(cron = "0 30 5 * *")
//    public void WeekReport()
//    {
//
//    }
}
